package com.wzh.user.aop;

import com.alibaba.fastjson.JSONObject;
import com.wzh.user.vo.SysUserVo;
import com.wzh.user.entity.UserLogEntity;
import com.wzh.user.feign.LogFeign;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author WZH
 * @title: AdminAop
 * @projectName find_house
 * @description:
 * @date 2022/3/24
 */
@Component
@Aspect
public class AdminAop {

    @Autowired
    private LogFeign logFeign;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Pointcut("execution(* com.wzh.user.controller.HouseUserController.save(..))")
    public void savePointCut() {

    }
    @Pointcut("execution(* com.wzh.user.controller.HouseUserController.update(..))")
    public void updatePointCut() {

    }
    @Pointcut("execution(* com.wzh.user.controller.HouseUserController.delete(..))")
    public void deletePointCut() {

    }

    @After("savePointCut()")
    public void save(JoinPoint point) throws Throwable {
        UserLogEntity userLog = new UserLogEntity();
        userLog.setCreateDate(new Date());
        String userJsonString = redisTemplate.opsForValue().get("user");
        SysUserVo sysUserVo = JSONObject.parseObject(userJsonString, SysUserVo.class);
        userLog.setUsername(sysUserVo.getUsername());
        userLog.setMethod(point.getSignature().getDeclaringTypeName()+'.'+point.getSignature().getName());
        userLog.setParams(JSONObject.toJSONString(point.getArgs()));
        userLog.setOperation(sysUserVo.getUsername()+"添加用户");
        logFeign.save(userLog);
    }

    @After("updatePointCut()")
    public void update(JoinPoint point) throws Throwable {
        UserLogEntity userLog = new UserLogEntity();
        userLog.setCreateDate(new Date());
        String userJsonString = redisTemplate.opsForValue().get("user");
        SysUserVo sysUserVo = JSONObject.parseObject(userJsonString, SysUserVo.class);
        userLog.setUsername(sysUserVo.getUsername());
        userLog.setMethod(point.getSignature().getName());
        userLog.setParams(JSONObject.toJSONString(point.getArgs()));
        userLog.setOperation(sysUserVo.getUsername()+"修改用户");
        logFeign.save(userLog);
    }
    @After("deletePointCut()")
    public void delete(JoinPoint point) throws Throwable {
        UserLogEntity userLog = new UserLogEntity();
        userLog.setCreateDate(new Date());
        String userJsonString = redisTemplate.opsForValue().get("user");
        SysUserVo sysUserVo = JSONObject.parseObject(userJsonString, SysUserVo.class);
        userLog.setUsername(sysUserVo.getUsername());
        userLog.setMethod(point.getSignature().getName());
        userLog.setParams(JSONObject.toJSONString(point.getArgs()));
        userLog.setOperation(sysUserVo.getUsername()+"删除用户");
        logFeign.save(userLog);
    }
}
